<?php

namespace App\Libs\Company\SheetTemplateHandler\Aggregate;

use App\Libs\Company\SheetTemplateHandler\Manager;
use App\Models\AggregateReport;
use App\Models\SheetTemplate;
use App\Models\Task;

class Template224 extends AbstractTemplate
{
    public $dataStartRow = 2;

    protected $lastMonthData;

    protected static $monthColumnMap = [
        '1' => 1,
        '2' => 2,
        '3' => 3,
        '4' => 4,
        '5' => 5,
        '6' => 6,
        '7' => 7,
        '8' => 8,
        '9' => 9,
        '10' => 10,
        '11' => 11,
        '12' => 12,
    ];

    public function generateAbstract()
    {
        $this->renderHeader();
        $this->output['sheets']['sheet-01']['cellData']['0']['0'] = $this->createTitleCell("日喀则市{$this->task->year}年1--12月份\r{$this->template->name}");

        $this->lastMonthData = $this->getLastMonthData();

        // 区县数据
        $task = Task::where([
            'sheet_template_id' => 112,
            'year' => $this->task->year,
            'key' => $this->task->key,
            'frequency' => $this->task->frequency,
            'status' => Task::STATUS_COMPLETED,
        ])
            ->first();

        if ($task) {
            foreach ($task->subTasks as $subTask) {
                $cellData = $subTask->data['sheets']['sheet-01']['cellData'];
                $currMonthValue = (int) $cellData[7][2]['v'] + (int) $cellData[8][2]['v'];

                $newRow = [
                    ['v' => str_replace('旅游局', '', $subTask->district->name), 's' => 'BOLD_CENTER'],
                ];
                foreach (self::$monthColumnMap as $month => $column) {
                    if ($month == $task->key) {
                        $newRow[$column] = ['v' => $currMonthValue, 't' => 2];
                        break;
                    }
                    if (isset($this->lastMonthData[$newRow[0]['v']])) {
                        $newRow[$column] = ['v' => $this->lastMonthData[$newRow[0]['v']][$column]['v'] ?? 0, 't' => 2];
                    }
                }
                $newRow[13] = ['f' => '=SUM(B' . $this->dataEndRow + 1 . ':M' . $this->dataEndRow++ + 1 . ')', 't' => 2];
                $this->output['sheets']['sheet-01']['cellData'][] = $newRow;
            }
        }

        // 旅行社数据
        $task = Task::where([
            'sheet_template_id' => 111,
            'year' => $this->task->year,
            'key' => $this->task->key,
            'frequency' => $this->task->frequency,
            'status' => Task::STATUS_COMPLETED,
        ])
            ->first();

        if ($task) {
            $currMonthValue = 0;
            foreach ($task->subTasks as $subTask) {
                $handler = Manager::createReportHandler(SheetTemplate::find(111), $subTask);
                $data = $handler->getData();
                foreach ($data as $row) {
                    $currMonthValue += (int) $row[2]['v'];
                }
            }

            $newRow = [
                ['v' => '旅行社', 's' => 'BOLD_CENTER'],
            ];
            foreach (self::$monthColumnMap as $month => $column) {
                if ($month == $task->key) {
                    $newRow[$column] = ['v' => $currMonthValue, 't' => 2];
                    break;
                }
                if (isset($this->lastMonthData[$newRow[0]['v']])) {
                    $newRow[$column] = ['v' => $this->lastMonthData[$newRow[0]['v']][$column]['v'] ?? 0, 't' => 2];
                }
            }
            $newRow[13] = ['f' => '=SUM(B' . $this->dataEndRow + 1 . ':M' . $this->dataEndRow++ + 1 . ')', 't' => 2];
            $this->output['sheets']['sheet-01']['cellData'][] = $newRow;
        }

        $this->dataEndRow--;
        $this->renderFooter();

        return $this->output;
    }

    public function getLastMonthData()
    {
        if ($this->task->key == 1) {
            $year = $this->task->year - 1;
            $key = 12;
        } else {
            $year = $this->task->year;
            $key = (int) $this->task->key - 1;
        }

        $lastAggregateReport = AggregateReport::where([
            'sheet_template_id' => $this->template->id,
            'frequency' => $this->task->frequency,
            'year' => $year,
            'key' => $key,
        ])
            ->first();
        if (!$lastAggregateReport) {
            return [];
        }

        $data = [];
        $i = $this->dataStartRow;
        while (1) {
            $row = $lastAggregateReport->data['sheets']['sheet-01']['cellData'][$i++] ?? [];
            if (!isset($row[0]['v'])) {
                break;
            }
            $data[$row[0]['v']] = $row;
        }
        return $data;
    }
}
